package cn.exrick.front.controller;

import cn.exrick.common.constant.CommonConstant;
import cn.exrick.common.pojo.DataTablesResult;
import cn.exrick.common.pojo.ZTreeNode;
import cn.exrick.manager.dto.front.Cart;
import cn.exrick.manager.dto.front.CartProduct;
import cn.exrick.manager.pojo.*;
import cn.exrick.manager.service.*;
import cn.exrick.sso.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private ContentService contentService;
    @Autowired
    private CartService cartService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private SysSettingService sysSettingService;

    @RequestMapping("/")
    public String index(Model model,HttpSession session){
        //轮播图
        List<TbPanelContent> panelContentList = contentService.getPanelContentList();
        model.addAttribute("panelContentList",panelContentList);
        //推荐商品
        List<TbItem> tbItemList=contentService.getRecommendGoods();
        model.addAttribute("recommendGoodList",tbItemList);

        setAttribute(model,session);

        return "index";
    }

    private void setAttribute(Model model,HttpSession session){
        //商品分类
        List<ZTreeNode> categoryList = itemCatService.getItemCatTree(null);
        model.addAttribute("categoryList",categoryList);
        //导航栏
        List<TbNav> navList=contentService.getNavList();
        model.addAttribute("navList",navList);
        //系统设置
        TbSysSetting setting = sysSettingService.getSetting();
        model.addAttribute("sysSetting",setting);
        //资讯
        DataTablesResult result = newsService.getNewsList(0, 0, 5, new TbNews(), "update_time", "desc");
        model.addAttribute("newsList",result.getData());

        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        if(member != null){
            model.addAttribute("member",member);
        }
    }


    @RequestMapping("/index/cart")
    public String cart(Cart cart,Model model, HttpSession session){
        //购物车
        TbMember member = (TbMember) session.getAttribute(CommonConstant.USER_SESSION);
        List<CartProduct> cartProductList=cartService.getCartList(member.getId());
        model.addAttribute("cartProductList",cartProductList);
        return "cart";
    }

    @RequestMapping("/index/newsList")
    public String newsList(Model model,HttpSession session){
        setAttribute(model,session);
        return "news-list";
    }

    @RequestMapping("/index/getNewsList")
    @ResponseBody
    public DataTablesResult getNewsList(int start,int length){
        TbNews tbNews = new TbNews();
        tbNews.setStatus(CommonConstant.NEWS_STATUS_RELEASE);
        DataTablesResult result = newsService.getNewsList(0, start, length, tbNews, "a.update_time", "desc");
        //去除html标签
        List<TbNews> newsList = (List<TbNews>)result.getData();
        for(TbNews news : newsList){
            news.setContent(news.getContent().replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", ""));
        }
        return result;
    }

    @RequestMapping("/index/news")
    public String getNewsByNavId(Long id,Model model,HttpSession session){
        TbNews tbNews = newsService.getNewsById(id);
        model.addAttribute("tbNews",tbNews);
        setAttribute(model,session);
        return "news";
    }

    @RequestMapping("/index/article")
    public String article(Long navId,Model model,HttpSession session){
        TbArticle tbArticle = articleService.getArticleByNavId(navId);
        TbNav nav = contentService.getNavById(navId);
        TbNav parentNav = contentService.getNavById(nav.getParentId());
        model.addAttribute("tbArticle",tbArticle);
        model.addAttribute("navName",nav.getName());
        model.addAttribute("navParentName",parentNav.getName());
        setAttribute(model,session);
        return "article";
    }

}
